<?php

namespace App\Http\Validate\Kms\Album;

use App\Http\Validate\AbstractBaseValidate;
use App\Models\Kms\Album\AlbumMainModel;
use App\Models\Kms\Album\AlbumResourceModel;
use App\Service\Kms\Album\AlbumMainService;
use App\Service\OpenShop\Feishu\ConfigService;
use Illuminate\Support\Carbon;
use Illuminate\Support\MessageBag;
use Illuminate\Validation\Rule;
use Illuminate\Validation\ValidationException;
use Psr\Container\ContainerExceptionInterface;
use Psr\Container\NotFoundExceptionInterface;

class AlbumMainValidate extends AbstractBaseValidate
{
    function getRules(): array
    {
        // TODO: Implement getRules() method.
        return [
            "mid" => [
                "required",
                "int",
                Rule::exists((new AlbumMainModel())->getTable(), "id")
                    ->where("ownerUid", request()->user()->rmsUserId)
            ]
        ];
    }

    function getAttributes(): array
    {
        // TODO: Implement getAttributes() method.
        return [
            "mid"                                  => "资源主信息id",
            "albumResourceGroup.*.weather"         => "天气",
            "albumResourceGroup.*.creatorUsername" => "创建人昵称",
            "albumResourceGroup.*.type"            => "资源类型",
            "albumResourceGroup.*.originalUrl"     => "资源原始路径",
            "albumResourceGroup.*.previewUrl"      => "资源预览路径",
            "albumResourceGroup.*.long"            => "经度",
            "albumResourceGroup.*.lat"             => "纬度",
            "albumResourceGroup.*.addressPrefix"   => "地址前缀 - 省市区",
            "albumResourceGroup.*.addressSuffix"   => "地址后缀 - 详细地址",
            "albumResourceGroup.*.videoDuration"   => "视频时长",
            "albumResourceGroup.*.size"            => "required|int|gt:0",
            "albumResourceGroup.*.shootingTime"    => "拍摄时间",
            "albumResourceGroup.*.poiSource"       => "poi来源",
            "albumResourceGroup.*.poiNo"           => "poi编号",
            "albumResourceGroup.*.poiName"         => "poi名称",
            "albumResourceGroup.*.resolutionRatio" => "资源分辨率"
        ];
    }

    function registerValidateRules(): void
    {
        // TODO: Implement registerValidateRules() method.
    }

    function getMessages(): array
    {
        // TODO: Implement getMessages() method.
        return [];
    }


    /**
     * @param array $params
     * @return MessageBag|object
     * @throws ValidationException
     * @throws ContainerExceptionInterface
     * @throws NotFoundExceptionInterface
     */
    public static function groupShareValidate(array $params)
    {
        $groupIdsStr = collect(AlbumMainService::groupList(false)["groupList"])->implode("id", ",");
        return self::getInstance()->validate(
            ["groupIds", "resourceIds", "mid", "comment"],
            $params,
            [
                "resourceIds"   => ["required", "array", "max:9"],
                "resourceIds.*" => [
                    "required",
                    "int",
                    "gt:0",
                    Rule::exists((new AlbumResourceModel())->getTable(), "id")
                        ->where("mid", attributeGet($params, "mid", 0))
                ],
                "groupIds"      => [
                    "required",
                    "array"
                ],
                "groupIds.*"    => [
                    "int",
                    "in:" . $groupIdsStr
                ],
                "comment"       => "string"
            ]
        );
    }
}
